﻿using System.Application;
using System.Collections.Generic;
using System.Domain.Entity;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Ubiquity.Core.Extend;
using Ubiquity.Core.Web;
using Ubiquity.Core.Web.Tree;
using Ubiquity.Core.Web.TreeGrid;
using Ubiquity.Web.Filters;

namespace Ubiquity.Web.Areas.SystemManage.Controllers
{
    /// <summary>
    ///组织表 控制器 
    /// </summary>	
    [Area("SystemManage")]
    public class SysOrganizeController : BaseController
    {
        private readonly SysOrganizeService _service;
         
        public SysOrganizeController(SysOrganizeService service)
        {
            _service = service;

        }

        /// <summary>
        ///  得到所有 数据 Dictionary 格式
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetDictionaryJson()
        {
            var data = _service.GetList();
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            foreach (var item in data)
            {
                var fieldItem = new
                {
                    encode = item.EnCode,
                    fullname = item.FullName
                };
                dictionary.Add(item.Id, fieldItem);
            }

            return Content(dictionary.ToJson());
        }

        /// <summary>
        /// 树形下拉选择菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetTreeSelectJson()
        {
            var data = _service.GetList();
            var treeList = new List<TreeSelectModel>();
            foreach (SysOrganizeEntity item in data)
            {
                TreeSelectModel treeModel = new TreeSelectModel();
                treeModel.id = item.Id;
                treeModel.text = item.FullName;
                treeModel.parentId = item.ParentId;
                treeModel.data = item;
                treeList.Add(treeModel);
            }

            return Content(treeList.TreeSelectJson());
        }

        /// <summary>
        /// 公司 树形下拉选择菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetCompanyTreeSelectJson()
        {
            var data = _service.GetCompanyList("0");
            var treeList = new List<TreeSelectModel>();
            foreach (SysOrganizeEntity item in data)
            {
                TreeSelectModel treeModel = new TreeSelectModel();
                treeModel.id = item.Id;
                treeModel.text = item.FullName;
                treeModel.parentId = item.ParentId;
                treeModel.data = item;
                treeList.Add(treeModel);
            }

            return Content(treeList.TreeSelectJson());
        }

        /// <summary>
        /// 部门 树形下拉选择菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetDepartmentTreeSelectJson(string parentId)
        {
            var data = _service.GetDepartmentList(parentId);
            var treeList = data.Select(item => new TreeSelectModel
            {
                id = item.Id,
                text = item.FullName,
                parentId = item.ParentId,
                data = item
            }).ToList();
            return Content(treeList.TreeSelectJson());
        }

        /// <summary>
        /// 职位 树形下拉选择菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetWorkTreeSelectJson(string parentId)
        {
            var data = _service.GetWorkList(parentId);
            var treeList = data.Select(item => new TreeSelectModel
            {
                id = item.Id,
                text = item.FullName,
                parentId = item.ParentId,
                data = item
            }).ToList();
            return Content(treeList.TreeSelectJson());
        }

        [HttpGet]
        [Authorize]
        public ActionResult GetTreeGridJson(string keyword)
        {
            var data = _service.GetList();
            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.TreeWhere(t => t.FullName.Contains(keyword));
            }

            var treeList = new List<TreeGridModel>();
            foreach (SysOrganizeEntity item in data)
            {
                TreeGridModel treeModel = new TreeGridModel();
                bool hasChildren = data.Count(t => t.ParentId == item.Id) != 0;
                treeModel.id = item.Id;
                treeModel.isLeaf = hasChildren;
                treeModel.parentId = item.ParentId;
                treeModel.expanded = hasChildren;
                treeModel.entityJson = item.ToJson();
                treeList.Add(treeModel);
            }

            return Content(treeList.TreeGridJson());
        }
        //[HttpGet]
        //[HandlerAjaxOnly]
        //public ActionResult GetGridPaginationJson(Pagination pagination)
        //{
        //    var data = new
        //    {
        //        rows = _app.GetList(pagination),
        //        pagination.total,
        //        pagination.page,
        //        pagination.records
        //    };
        //    return Content(data.ToJson());
        //}

        //[HttpGet]
        //[HandlerAjaxOnly]
        //public ActionResult GetGridJson()
        //{
        //    var data = _app.GetList();
        //    return Content(data.ToJson());
        //}
        [HttpGet]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = _service.GetEntity(keyValue);
            return Content(data.ToJson());
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult Insert(SysOrganizeEntity entity, string keyValue)
        {
            return _service.Insert(entity) ? Success("操作成功。") : Error("操作失败。");
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult Update(SysOrganizeEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Id = keyValue;
                return _service.Update(entity) ? Success("操作成功。") : Error("操作失败。");
            }

            return Error("主键验证错误");
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            if (_service.Delete(keyValue))
            {
                return Success("操作成功。");
            }
            else
            {
                return Error("操作失败。");
            }
        }
    }
}